﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;

namespace SlovotvorchiKontexty
{
    class Parser
    {
        public void ProcessFile(FileInfo fi)
        {
            Console.WriteLine("\nProcessing " + fi.Name);
            XmlDocument mydoc = null;
            try
            {
                mydoc = new XmlDocument();
                mydoc.Load(fi.FullName);

                  TextWriter tw_out = new StreamWriter("./out/"+fi.Name);



                XmlNodeList nodes = mydoc.SelectNodes("//text");
                int numSents = 0;
                Console.WriteLine("nodes count = " + nodes.Count);
                for (int num = 0; num < nodes.Count; num++)
                {

                    string strSource = nodes[num].InnerText;
                    //Console.WriteLine(strSource);
                  //  string mask = @"(?<word> \w+)ness .*(\k<word>)";
                    string mask = @"(?<word> \w*) ((.)|(\n)){0,320}(\k<word>)ness";
                    //string mask = "(?<word>ness)";
                    Regex r = new Regex(mask, RegexOptions.Multiline|RegexOptions.IgnoreCase);
                    Match match = r.Match(strSource);
                    int matchCount = 0;
           
                    while (match.Success)
                    {
                        matchCount++;
                        tw_out.WriteLine(":\t" + match.Groups[0].Captures[0]);
                      //tw_out.WriteLine(strSource + "\n");
                        match = match.NextMatch();
                        
                    }

                    mask = @"(?<word> \w+)ness ((.)|(\n)){0,320}(\k<word>)((?!ness).)+";
                    r = new Regex(mask, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                    match = r.Match(strSource);
                    
          
                    while (match.Success)
                    {
                        matchCount++;
                        //Console.WriteLine(":\t" + matchCount.ToString());
                        tw_out.WriteLine(":\t" + match.Groups[0].Captures[0]);
                       // tw_out.WriteLine(strSource + "\n");
                        match = match.NextMatch();
                        
                    }
                    
                    //two identical nesses

                    mask = @"(?<word> \w+)ness ((.)|(\n)){0,320}(\k<word>)ness";
                    r = new Regex(mask, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                    match = r.Match(strSource);


                    while (match.Success)
                    {
                        matchCount++;
                        //Console.WriteLine(":\t" + matchCount.ToString());
                        tw_out.WriteLine("!!!\t" + match.Groups[0].Captures[0]);
                       // tw_out.WriteLine(strSource + "\n");
                        match = match.NextMatch();
                    //
                    }

                    if (matchCount > 0)
                        //numSents++;
                        numSents += matchCount;
                    
                    
                   
                }
                tw_out.WriteLine("Totoal number of sentences with ness + in " + fi.Name + " = " + numSents.ToString());
                tw_out.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Something went wrong:");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine();

                //Console.WriteLine("file invalid");
            }

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello!");
            DirectoryInfo di = new DirectoryInfo("./in");
            FileInfo[] Files = di.GetFiles();
            try
            {
                if (Directory.Exists("./out")) Directory.Delete("./out", true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("./out :" + ex.Message);
            }
            try { Directory.CreateDirectory("./out"); }
            catch (Exception ex) { Console.WriteLine("./out :" + ex.Message); };

            Parser parser = new Parser();

            foreach (FileInfo fi in Files)
            {
                parser.ProcessFile(fi);
            }

        }
    }
}
